/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.com.digitalnet.ad.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;

import cn.com.digitalnet.ad.base.CrudService;
import cn.com.digitalnet.ad.entity.Permission;
import cn.com.digitalnet.ad.entity.Role;
import cn.com.digitalnet.ad.entity.User;
import cn.com.digitalnet.ad.repository.mybatis.UserMao;

import com.google.common.collect.Maps;

/**
 * 用户管理类. 
 * 
 * @author calvin
 */
// Spring Service Bean的标识.
@Service
@Transactional
public class UserService extends CrudService<User>{

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	@Autowired private UserMao mao;
	
	@Override
	protected UserMao mao() {
		return mao;
	}
	
	@Override
	public void save(User user) {
		entryptPassword(user);
		mao.save(user);

		//添加新角色
		for(Role r : user.getRoleList()){
			Map<String, Object> params = Maps.newHashMap();
			params.put("userId", user.getId());
			params.put("roleId", r.getId());
			mao.addRoleForUser(params);
		}
		//添加新客户
		
	}
	
	@Override
	public void update(User user) {
		//清除原有角色
		mao.cleanRolesForUser(user.getId());
		//添加新角色
		for(Role r : user.getRoleList()){
			Map<String, Object> params = Maps.newHashMap();
			params.put("userId", user.getId());
			params.put("roleId", r.getId());
			mao.addRoleForUser(params);
		}
		//清除原有客户
		mao.cleanAccountsForUser(user.getId());
		//添加新客户
		
		//加密存储密码
		if (StringUtils.isNotBlank(user.getPlainPassword())) {
			entryptPassword(user);
		}
		mao.update(user);
	}

	public User findUserByLoginName(String loginName) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("loginName_eq", loginName);
		params.put("isenabled", 1L);
		return this.findOne(params);
	}

	public User findUserByName(String name) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("name_eq", name);
		params.put("isenabled", 1L);
		return this.findOne(params);
	}
	
	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(User user) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		user.setSalt(Encodes.encodeHex(salt));

		byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS);
		user.setPassword(Encodes.encodeHex(hashPassword));
	}

	public List<Role> getRolesForUser(Long userId){
		return mao.getRolesForUser(userId);
	}

	public List<Permission> getPermissionsForUser(Long userId){
		return mao.getPermissionsForUser(userId);
	}
	
	

	//返回map以 id ： name 的键值对
	public Map findToMap() {
		// TODO Auto-generated method stub
		Map map = Maps.newHashMap();
		List<User> users = findAll();
		for(User u : users){
			map.put(u.getId(), u.getName());
		}
		return map;
	}
	
}
